/*
 * Copyright (C) 2016 venshine.cn@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.wx.goodview;


import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.BaseDialog;
import ohos.agp.window.dialog.PopupDialog;
import ohos.app.Context;

/**
 * 点赞效果
 *
 * @author venshine
 */
public class GoodView extends PopupDialog implements IGoodView {

    private String mText = TEXT;

    private int mTextColor = TEXT_COLOR;

    private int mTextSize = TEXT_SIZE;

    private int mFromY = FROM_Y_DELTA;

    private int mToY = TO_Y_DELTA;

    private float mFromAlpha = FROM_ALPHA;

    private float mToAlpha = TO_ALPHA;

    private int mDuration = DURATION;

    private int mDistance = DISTANCE;

    private AnimatorProperty mAnimationSet;

    private boolean mChanged = false;

    private Context mContext = null;

    private Text mGood = null;

    private DependentLayout layout;

    private int mWidth, mHeight;

    public GoodView(Context context, Component contentComponent) {
        super(context, contentComponent);
        mContext = context;
        initView();
    }

    public GoodView(Context context, Component contentComponent, int width, int height) {
        super(context, contentComponent, width, height);
        mWidth = width;
        mHeight = height;
        mContext = context;
        initView();
    }

    private void initView() {
        setBackColor(Color.TRANSPARENT);
        layout = new DependentLayout(mContext);
        DependentLayout.LayoutConfig params =
                new DependentLayout.LayoutConfig(DependentLayout.LayoutConfig.MATCH_CONTENT,
                        DependentLayout.LayoutConfig.MATCH_CONTENT);
        params.addRule(DependentLayout.LayoutConfig.CENTER_IN_PARENT);
        params.addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_BOTTOM);
        mGood = new Text(mContext);
        mGood.setPaddingForText(false);
        mGood.setTextSize(mTextSize);
        mGood.setTextColor(new Color(mTextColor));
        mGood.setText(mText);
        mGood.setLayoutConfig(params);
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(new RgbColor(255, 255, 255, 0));
        layout.setBackground(shapeElement);
        layout.addComponent(mGood);
        setCustomComponent(layout);
        LogUtil.error(this.getClass().getName(), "1====initView====");
    }


    public PopupDialog setText(String text) {
        mText = text;
        mGood.setText(text);
        mGood.setTextSize(30);
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(new RgbColor(255, 255, 255, 0));
        mGood.setBackground(shapeElement);
        return this;
    }



    private void setTextColor(int color) {
        mTextColor = color;
        mGood.setTextColor(new Color(color));
    }


    private void setTextSize(int textSize) {
        mTextSize = textSize;
        mGood.setTextSize(textSize);
    }


    public void setTextInfo(String text, int textColor, int textSize) {
        setTextColor(textColor);
        setTextSize(textSize);
        setText(text);
    }



    public void setImage(Element drawable) {
        if (drawable == null) {
            throw new IllegalArgumentException("drawable cannot be null.");
        }
        mGood.setBackground(drawable);
        mGood.setWidth(50);
        mGood.setHeight(50);
        mGood.setText("");
        LogUtil.error(this.getClass().getName(), "1====setImage====");
    }


    public void setDistance(int dis) {
        mDistance = dis;
        mToY = dis;
        mChanged = true;
//        setHeight(mDistance + mGood.getMeasuredHeight());
    }


    public void setTranslateY(int fromY, int toY) {
        mFromY = fromY;
        mToY = toY;
        mChanged = true;
    }

    /**
     * 设置透明度属性
     *
     * @param fromAlpha
     * @param toAlpha
     */
    public void setAlpha(float fromAlpha, float toAlpha) {
        mFromAlpha = fromAlpha;
        mToAlpha = toAlpha;
        mChanged = true;
    }


    public BaseDialog setDuration(int duration) {
        mDuration = duration;
        mChanged = true;
        return this;
    }

    /**
     * 重置属性
     */
    public void reset() {
        mText = TEXT;
        mTextColor = TEXT_COLOR;
        mTextSize = TEXT_SIZE;
        mFromY = FROM_Y_DELTA;
        mToY = TO_Y_DELTA;
        mFromAlpha = FROM_ALPHA;
        mToAlpha = TO_ALPHA;
        mDuration = DURATION;
        mDistance = DISTANCE;
        mChanged = false;
    }


    public void show(Component v, int offsetX, int offsetY) {
        if (!isShowing()) {
            if (mAnimationSet == null) {
                mAnimationSet = mGood.createAnimatorProperty();
                mAnimationSet.moveFromY(0).moveByY(-80).alpha(0).setDuration(mDuration).setDelay(200);
                LogUtil.error(this.getClass().getName(), "1====createAnimation===1223====tag");
            }
            mAnimationSet.setStateChangedListener(new Animator.StateChangedListener() {
                @Override
                public void onStart(Animator animator) {
                    LogUtil.error(this.getClass().getName(), "1====createAnimation===1223");
                }

                @Override
                public void onStop(Animator animator) {

                }

                @Override
                public void onCancel(Animator animator) {

                }

                @Override
                public void onEnd(Animator animator) {
                    LogUtil.error(this.getClass().getName(), "1====createAnimation===111===" + isShowing());
                    if (isShowing()) {
                        hide();
                    }
                }

                @Override
                public void onPause(Animator animator) {

                }

                @Override
                public void onResume(Animator animator) {

                }
            });
            if (offsetX == 0) offsetX = v.getLocationOnScreen()[0] + 150;
            if (offsetY == 0) offsetY = v.getLocationOnScreen()[1] + 30;
            setOffset(offsetX, offsetY);
            show();
            mAnimationSet.start();
        }
    }
}
